//
//  ADViewController.m
//  j绝版新闻客户端
//
//  Created by weige on 16/5/9.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "ADViewController.h"

@interface ADViewController ()
{
    UIImageView *backgroudImage;
}
@end

@implementation ADViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self LoadImageViewInit];
    [UIApplication sharedApplication].statusBarHidden=YES;
    [self.view addSubview:backgroudImage];
}


//构建一个view
-(void)LoadImageViewInit
{
    backgroudImage=[[UIImageView alloc]initWithFrame:ScreenBounds];
   
    //NSLog(@"%@",ad.img_name);
    
    ADModel *admodel=[self loadDataImage];
    if (admodel.img_name!=nil) {

       // SDWebImageManager
        [backgroudImage  sd_setImageWithURL:[NSURL URLWithString:admodel.img_name] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            //是否img为空
            if (image!=nil) {
                //设置线程延迟时间
             dispatch_time_t time= dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0* NSEC_PER_SEC));
                dispatch_after(time, dispatch_get_main_queue(), ^{
                    
                   [UIApplication sharedApplication].statusBarHidden=YES;
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [[NSNotificationCenter defaultCenter]postNotificationName:ADImageLoadSecussed  object:image];
                    });
                });
            }
            else
            {
                //加载失败
                NSLog(@"加载失败");
                 [[NSNotificationCenter defaultCenter]postNotificationName:ADImageLoadFail  object:admodel];
            }
        } ];
    }
    
    
    
}


-(ADModel *)loadDataImage
{
    NSString *path=[ContentFileFillPath ContentFileFillPaths:@"AD"];
    NSData *data=[NSData dataWithContentsOfFile:path];
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    ADModel *ad=[ADModel mj_objectWithKeyValues:dict];
    return ad;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
